import Vue from 'vue'
import Router from 'vue-router'
import appConfig from '@/utils/appConfig'
import store from '@/store'

Vue.use(Router)

/* Layout */
import Layout from '../views/layout/Layout'

export const routerMap = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index'),
  },

  {
    path: '',
    hidden: true,
    component: Layout,
    meta: {
      auth: true,
    },
    children: [
      {
        path: '/',
        redirect: appConfig.index_redirect ? appConfig.index_redirect : undefined,
        component: () => import('@/builder/Content'),
      },
      {
        path: '*',
        component: () => import('@/builder/Content'),
      },
    ],
  },
]

const router = new Router({
  mode: 'history',
  base: appConfig.prefix,
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    ...routerMap,
  ],
})

router.beforeEach((to, from, next) => {
  store.commit('NOT_FOUND', false)

  next()
})

export default router
